package com.zzptc.easyshop.dao;

import com.zzptc.easyshop.db.IDB;
import com.zzptc.easyshop.db.MysqlDB;
import com.zzptc.easyshop.facory.DaoFacoryImp;
import com.zzptc.easyshop.vo.Product;
import com.zzptc.easyshop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodProDaoImp implements IDAO{
    @Override
    public boolean insert(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean login(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean deleteByID(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean update(VO vo) throws SQLException {
        return false;
    }

    @Override
    public VO getByID(VO vo) throws SQLException {
        return null;
    }

    public Product getGoodByID(int id)throws SQLException{
        Connection conn=null;//连接对象
        PreparedStatement ps = null;//操作数据库的工具
        ResultSet rs = null;//结果集
        Product product=new Product();//定义一个商品实体对象 用于存储数据
        IDB db=new MysqlDB();
        try {
            conn = db.getConn();//获取数据库链接，获取工具PrepareStatement
            ps=conn.prepareStatement("select * from easybuy_product where ep_id=?");
            ps.setInt(1,id); //给“？”注入值
            rs=ps.executeQuery();//操作数据库
            if (rs.next()) {//从结果集中将仅有的一行数据注入到实体类对象中
                Product product1=new Product();
                product.setEp_id(rs.getInt(1));
                product.setEp_name(rs.getString(2));
                product.setEp_description(rs.getString(3));
                product.setEp_price(rs.getFloat(4));
                product.setEp_stock(rs.getInt(5));
                product.setEpc_id(rs.getInt(6));;
                product.setEpc_child_id(rs.getInt(7));
                product.setEp_file_name(rs.getString(8));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {//释放资源，关闭数据库
            try {
                rs.close();
                ps.close();
                db.getConn();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return product;//返回实体类对象
    }
    @Override
    public List getAll() throws SQLException {
//        MysqlDB db=new MysqlDB();
//        Connection conn=null;
//        conn=db.getConn();
//        PreparedStatement ps=conn.prepareStatement("select * from easybuy_product");
//        ResultSet rs=ps.executeQuery();
//        List<Product> list=new ArrayList();
//        while(rs.next()){
//            Product product=new Product();
//            product.setEp_id(rs.getInt(1));
//            product.setEp_name(rs.getString(2));
//            product.setEp_description(rs.getString(3));
//            product.setEp_price(rs.getFloat(4));
//            product.setEp_stock(rs.getInt(5));
//            product.setEpc_id(rs.getInt(6));;
//            product.setEpc_child_id(rs.getInt(7));
//            product.setEp_file_name(rs.getString(8));
//            list.add(product);
//        }

        return null;
    }

    public static void main(String[] args) throws SQLException {
//        GoodProDaoImp dao = null;
//        try {
//            dao=(GoodProDaoImp) DaoFacoryImp.getInstance().createDAO("productdao");
//            System.out.println(dao.getAll().size());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        GoodProDaoImp goodProDaoImp=new GoodProDaoImp();
        System.out.println(goodProDaoImp.getGoodByID(3).getEp_name());
    }

}